<template>
    <div id="app">
        <m-header :search=true :beforedown=true @search='goSearch' @downgame='goDownPage'></m-header>
        <tab></tab>
        <keep-alive>
            <router-view></router-view>
        </keep-alive>
    </div>
</template>

<script>
import MHeader from 'components/m-header/m-header';
import Tab from 'components/tab/tab';
import { getCommonInfoHasData, postInfo } from '@/base/api/indexpage/getindexinfo';
import { mapMutations, mapGetters } from 'vuex';

export default {
    created() {
        if (window.plus) {
            setTimeout(this.init(), 0);
        } else {
            document.addEventListener('plusready', () => {
                this.init();
            }, false);
        }
        this._getUserLoginInfo();
    },
    methods: {
        init() {
            postInfo('/api/init', {
                imei: plus.device.imei,
                unionId: '5ac1db94209f9',
            }).then(() => { });
        },
        goSearch() {
            this.$router.push({
                path: '/index/search',
                query: {
                    address: 'index',
                },
            });
        },
        goDownPage() {
            this.$router.push({
                path: '/index/down',
            });
        },
        _getUserLoginInfo() {
            const userLoginAddress = '/user/info';
            console.log(this.getToken);
            if (this.getToken === '' || this.getToken === null) {
                this.userLogout();
            } else {
                getCommonInfoHasData(userLoginAddress, { token: this.getToken }).then((res) => {
                    console.log(res);
                    if (!res.status_code) {
                        this.actived = true;
                        this.userInfo = res.data;
                        this.userLoginState();
                        this.setusername(res.data.username);
                        this.setnickname(res.data.nickname);
                        this.setage(res.data.age);
                        this.setmobile(res.data.mobile);
                        this.setavatar(res.data.avatar);
                        this.setsex(res.data.sex);
                    } else {
                        this.userLogout();
                    }
                });
            }
        },
        ...mapMutations([
            'userLoginState',
            'userLogout',
            'setusername',
            'setnickname',
            'setage',
            'setmobile',
            'setavatar',
            'setsex',
        ]),
    },
    computed: {
        ...mapGetters(['getToken']),
    },
    components: {
        MHeader,
        Tab,
    },
};
</script>


<style scoped lang="stylus" rel="stylesheet/stylus">
</style>
